﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

public partial class MembershipCard_wechat_web_Products : WeiPage
{
    PRODUCTBO pbo = new PRODUCTBO();
    PersonInfoBO fbo = new PersonInfoBO();
    ShoppingCartBO sbo = new ShoppingCartBO();
    protected void Page_Load(object sender, EventArgs e)
    {
       // ReturnMessage("sss");
        if (!IsPostBack)
        {

            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=d9bfd969e149c1844698cfd1f962bdad&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = "wx8ecea9aaa3d6162b";
                       // Response.Write(b[13]);
                    }
                }
                catch (Exception)
                {
                  
                }
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
                PersonInfo per = fbo.GetModel(user);
                if (per != null)
                {
                    List<ShoppingCart> list2 = sbo.GetModelList(" pid=" + per.ID);
                    if (list2.Count > 0)
                    {
                        sumcar.InnerHtml = "<span><a href='ShopCar.aspx'><em class='indexicon4'></em><i>" + list2.Count + "</i></a></span>";
                    }
                }
            }

            string swhere=" IF_DISP=1 AND IF_REDP=1 ";
            if (Request.QueryString["mx"] != null)
                swhere += " and IF_STAR=1 ";
            if (Request.QueryString["xp"] != null)
                swhere += " and IF_NEW=1 ";
            if (Request.QueryString["top"] != null)
                swhere += " and IF_TOP=1 ";
            List<PRODUCT> list = pbo.GetModelList(swhere+" ORDER BY Integral");
            plist.InnerHtml = "";
            foreach (PRODUCT p in list)
            {
                plist.InnerHtml += string.Format(@"<ol>
   <p>
   <a href='ProductsExchange.aspx?id={3}' ><img src='{0}'  style='max-height:287px;' alt='' /></a>
   <span>&nbsp;&nbsp;{1}</span>
   <span>&nbsp;&nbsp;积分：<strong>{2}分</strong></span>
   </p>
   </ol>
            ", p.PicUrl==""?"images/m.png":p.PicUrl, GetSameLongName(p.Explain.Trim()), p.Integral, p.ID);
            }
        }
    }

    

    public string GetSameLongName(string name)
    {
        if (name.Length <=9)
            return name;
        return name.Substring(0,9)+"...";
    }
}